/*
    This file is part of libcloak.

    libcloak is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libcloak is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libcloak.  If not, see <http://www.gnu.org/licenses/>.

*/



#include <TextResource.h>
#include <XmlResource.h>
#include <ImageResource.h>
#include <Cloak.h>
#include <iostream>
#include <fstream>
using namespace Cloak;

class MyTestHandler : public Handler
{
public:
	MyTestHandler(string path) : Handler(path)
	{
	}

	virtual Entity handlePath(Manager &manager, PathVector &path, AttrMap &attrs)
	{
	    int *a = new int;

	    AttrMap::iterator it = attrs.find("num");
	    if(it == attrs.end())
	    {
            *a = 0xDEADBEEF;
	    }
	    else
        {
            *a = atoi(it->second.c_str());
        }
		return a;
	}
};

int main(int argc, char *argv[])
{
	bool success;

	TextResource textTest("./Doxyfile");
	cerr << "Loading Text" << endl;
	success = textTest.isLoaded();
	cerr << "Value: " << success << endl;

	XmlResource xmlTest("./tests/test.xml");
	cerr << "Loading XML" << endl;
	success = xmlTest.isLoaded();
	cerr << "Value: " << success << endl;

	ImageResource imageTest("./tests/img.gif");
	cerr << "Loading Image" <<endl;
	success = imageTest.isLoaded();
	cerr << "Value: " << success << endl;

	cerr << "Copying all" << endl;
	TextResource copyText(textTest);
	XmlResource copyXml(xmlTest);
	ImageResource copyImage(imageTest);


	Manager *man = new Manager();
	CLOAK_ADDHANDLER("entities/f", MyTestHandler,man);
	CLOAK_ADDHANDLER("entities/test",MyTestHandler,man);
	CLOAK_ADDHANDLER("entities/*",MyTestHandler,man);
	CLOAK_ADDHANDLER("*",MyTestHandler,man);

	man->debugTree();

	try{
	CloakRef<const int> r1 = man->getEntity<int>("entities/f","");
	CloakRef<const int> r2 = man->getEntity<int>("entities/firetruck.png","");

	//CloakRef<const double> r3 = man->getEntity<double>("entities/f","color:red");

	string ent = "entities/omghax";
	string e2 = "";
	for(int i=0; i<100000; i++)
		man->getEntity<int>(ent,e2);
	} CLOAK_CATCH_DEBUG

	CloakRef<const int> r3 = man->getEntity<int>("e1","color:red,num:5");

	CloakRef<const int> r4 = man->getEntity<int>("e1","color:red,num:5");

	man->debugCache();

	//man->garbageCollect(Manager::QUARTER_SLIDING);

	man->startGarbageThread(Manager::FULL, 100);
	sleep(1);
	man->stopGarbageThreads();

	cerr << "GARBAGE COLLECTED" << endl;

	man->debugCache();

	cerr << "Done Static Test" << endl;
    CloakRef<int> r5 = man->getMutableEntity<int>("e44","num:2");

    cerr << "This should be 2:" << *r5 << endl;
    *r5 = 4;
    cerr << "This should be 4:" << *r5 << endl;

    CloakRef<int> r6 = man->getEntityCopy<int>("e1","color:red");
    *r6 = 6;

    // Notice different attr order, this is to test that order doesn't matter
    cerr << "Value " << *man->getEntity<int>("e1","num:5,color:red") << " changed to " << *r6 << endl;

    cerr << "Done Dynamic Test" << endl;

	delete man;

	return 0;
}
